var auditUserList;
var copyToUserList;

$(function () {
    //表单验证
    $('#inputForm').validator().on('submit', function (e) {
        if (e.isDefaultPrevented()) {
            // handle the invalid form...
        } else {
            // 验证审批人
            var flag = validAuditList();
            if(!flag)
                return false;
            //转化抄送人集合
            exchangeCopyToUserIdsList();
            //防止重复提交
            FormUtil.blockRepeatSubmit($('#inputForm'), 2000);

            var paramArr = $("#inputForm").serialize()
            HttpUtil.ajaxAsyncJsonPost("/example/leave/add", paramArr, function(message){
                dialogTipSuccess(message);
                refreshPage(1500, '/static/bflow/ui/example/leave/list.html');
            });
            return false;
        }
    });

    //审批人集合插件
    var auditUserListUL = document.getElementById("auditUserListUL");
    auditUserList = Sortable.create(auditUserListUL, {
        group: "userGroup",
        animation: 150,
        onAdd: function (evt){ console.log('onAdd.bar:', evt.item); },
        onUpdate: function (evt){ console.log('onUpdate.bar:', evt.item); },
        onRemove: function (evt){ console.log('onRemove.bar:', evt.item); },
        onStart:function(evt){ console.log('onStart.foo:', evt.item);},
        onEnd: function(evt){ console.log('onEnd.foo:', evt.item);},
        filter: '.js-remove',
        onFilter: function (evt) {
            evt.item.parentNode.removeChild(evt.item);
        }
    });

    //抄送人集合插件
    var copyToUserListUL = document.getElementById("copyToUserListUL");
    copyToUserList = Sortable.create(copyToUserListUL, {
        group: "userGroup",
        animation: 150,
        onAdd: function (evt){ console.log('onAdd.bar:', evt.item); },
        onUpdate: function (evt){ console.log('onUpdate.bar:', evt.item); },
        onRemove: function (evt){ console.log('onRemove.bar:', evt.item); },
        onStart:function(evt){ console.log('onStart.foo:', evt.item);},
        onEnd: function(evt){ console.log('onEnd.foo:', evt.item);},
        filter: '.js-remove',
        onFilter: function (evt) {
            evt.item.parentNode.removeChild(evt.item);
        }
    });
});


//挑选 审批人
function chooseAuditUser(type){
    dialogOpenPageConfirm('选择活动', '/static/bflow/ui/define/assignUserList.html', 700, 400, function () {
        var iframe = $(document).find(".jconfirm-content iframe")[0].contentWindow;
        var userArray = iframe.retSelect();
        console.log("选择的用户为" + JSON.stringify(userArray));
        if(userArray != null && userArray.length > 0){
            for (var i = 0; i< userArray.length ; i++){
                var userObj = userArray[i];
                if(userObj != null){
                    var el = document.createElement('li');
                    if(type == 'auditUser'){
                        el.setAttribute("uid", userObj.userId);
                        el.innerHTML = userObj.userName + '<i class="js-remove">✖</i>';
                        auditUserList.el.appendChild(el);
                    }else if(type == 'copyToUser'){
                        el.setAttribute("uid", "_" + userObj.userId + "_");
                        el.innerHTML = userObj.userName + '<i class="js-remove">✖</i>';
                        copyToUserList.el.appendChild(el);
                    }
                }
            }
        }
        return true; //false则窗口不关闭
    }, function () {
        //init
    });
}

//验证审批人
function validAuditList() {
    var flag = true;
    var auditListIds = new Array();
    $("#auditUserListUL li").each(function (index) {
        auditListIds.push($(this).attr("uid"));
    });
    if(auditListIds.length == 0){
        dialogTipError("请至少选择一个审批人！");
        flag = false;
    }else {
        $("#auditUserIds").val(auditListIds.join(","));
    }
    return flag;
}

//转化抄送人集合
function exchangeCopyToUserIdsList() {
    var copyToUserIdsList = new Array();
    $("#copyToUserListUL li").each(function (index) {
        copyToUserIdsList.push($(this).attr("uid"));
    });
    $("#copyToUserIds").val(copyToUserIdsList.join(","));
}